package org.example;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Arrays;
import java.util.Properties;

public class kfkConsumer {
    public static void main(String[] args) {
        /*配置项
        1）bootstrap server ，key ，value 的反序列化
        2）创建kafka 消费者组，kafka 消费组名称
        3）订阅kafka 的topic
        4）通过循环的形式，打印

         */

        //配置的消费者 配置信息  服务地址，key value，id，位置信息
        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.80.136:9092");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "niit01");
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        //构建kafka
        KafkaConsumer<String, String> kfkaConsumer = new KafkaConsumer<String, String>(properties);

        //topic name test1
        //订阅topic name
        kfkaConsumer.subscribe(Arrays.asList("15homework"));

        //循环消费
        while (true) {
            ConsumerRecords<String, String> conRecord = kfkaConsumer.poll(5);

            //循环遍历
            for (ConsumerRecord<String, String> consumer : conRecord) {
                System.out.println("value is" + consumer.value());

            }

        }
    }}
